package com.pratices.cookie.util;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@WebServlet("/wx/storage/fetch/*")
public class WxStorageFetchServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取照片名字
        String url = req.getRequestURI();
        String imageName = url.substring(url.lastIndexOf("/") + 1);

        //获取文件
        File file = new File("D:\\myPratices\\daydayup\\javaee\\src\\main\\resources\\Image\\",imageName);
        FileInputStream fileInputStream = new FileInputStream(file);

        ServletOutputStream outputStream = resp.getOutputStream();

        //进行读取文件
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = fileInputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, len);
        }
        outputStream.close();
        fileInputStream.close();

    }
}
